package com.ebt.m.users;

import android.content.Context;
import android.text.TextUtils;

import com.ebt.m.AppInitService;
import com.ebt.m.commons.buscomponent.listview.BaseListContract;
import com.ebt.m.commons.buscomponent.listview.BaseListPresenter;
import com.ebt.m.commons.buscomponent.listview.SectionItem;
import com.ebt.m.commons.utils.NetUtils;
import com.ebt.m.commons.utils.RxUtil;
import com.ebt.m.data.rxModel.api.EBTAPI;
import com.ebt.m.users.bean.CardData;
import com.ebt.m.users.bean.CardHonour;
import com.ebt.m.users.bean.CardService;
import com.ebt.m.users.bean.CardTagData;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by andy.wang on 2016/12/14.
 */

public class CardDataPresenter extends BaseListPresenter<BaseListContract.IView> {
    @Override
    public List<SectionItem> createSectionItem(List datas) {
        return null;
    }

    public static final int TYPE_NORMAL_INFO = 1;
    public static final int TYPE_INTRODUCTION_TAG = 2;
    public static final int TYPE_INTRODUCTION = 3;
    public static final int TYPE_HONOUR_TAG = 4;
    public static final int TYPE_HONOUR = 5;
    public static final int TYPE_SERVICE_TAG = 6;
    public static final int TYPE_SERVICE = 7;


    public CardDataPresenter(Context context, BaseListContract.IView iView) {
        super(context, iView);
    }

    @Override
    public void loadNew(Object... args) {
        EBTAPI opportunityApi = AppInitService.getEbtApi();
        opportunityApi.getCardData()
                .compose(RxUtil.rxTransformer(iView))
                .subscribe(response -> iView.updateData(createSectionItem(response.getData()))
                        , throwable -> {
                            throwable.printStackTrace();
                            NetUtils.checkHttpException(mContext, throwable);
                            iView.errorData();
                        });
    }

    @Override
    public void refresh() {
        loadNew();
    }

    @Override
    public void loadNext(Object... args) {
    }

    public List<SectionItem> createSectionItem(CardData cardData) {
        List<SectionItem> sections = new ArrayList<SectionItem>();
        if (cardData == null) {
            return sections;
        }
        try {
            SectionItem infoItem = new SectionItem(TYPE_NORMAL_INFO, cardData);
            sections.add(infoItem);
            if (!TextUtils.isEmpty(cardData.getIntroduction())) {
                CardTagData tagData = new CardTagData();
                tagData.type = 0;
                tagData.showDivider = false;
                tagData.showEmpty = false;
                tagData.title = "个人介绍";
                sections.add(new SectionItem(TYPE_INTRODUCTION_TAG, tagData));
                sections.add(new SectionItem(TYPE_INTRODUCTION, cardData.getIntroduction()));
            }
            List<CardHonour> honours = cardData.getHonour();
            CardTagData tagData = new CardTagData();
            tagData.type = 1;
            tagData.title = "个人荣誉";
            SectionItem sectionItem1 = new SectionItem(TYPE_HONOUR_TAG, tagData);
            if (!TextUtils.isEmpty(cardData.getIntroduction())) {
                tagData.showDivider = true;
            }
            sections.add(sectionItem1);
            if (honours != null && honours.size() > 0) {
                tagData.showEmpty = false;
                for (int i = 0; i < honours.size(); i++) {
                    SectionItem sectionItem = new SectionItem(TYPE_HONOUR, honours.get(i));
                    if (i == 0)
                        sectionItem.extras = true;
                    sections.add(sectionItem);

                }
            } else {
                tagData.showEmpty = true;
            }
            List<CardService> services = cardData.getService();
            CardTagData tagData1 = new CardTagData();
            tagData1.type = 2;
            tagData1.title = "增员相册";
            tagData1.showDivider = true;
            SectionItem sectionItem = new SectionItem(TYPE_SERVICE_TAG, tagData1);
            sections.add(sectionItem);
            if (services != null && services.size() > 0) {
                tagData1.showEmpty = false;
                for (CardService service : services) {
                    sections.add(new SectionItem(TYPE_SERVICE, service));
                }
            } else {
                tagData1.showEmpty = true;
            }


        } catch (Exception e) {
            e.printStackTrace();
            return sections;
        }
        return sections;
    }

}
